package com.lebang.controller.sys.cences;

import com.lebang.common.Login;
import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.CencesDownloadMember;
import com.lebang.example.CencesDownloadMemberExample;
import com.lebang.model.MyConfig;
import com.lebang.service.cences.CencesDownloadMemberService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;


/**
 * 每日必赚统计
 *
 * @author hwq
 * @date 2020-02-10
 */
@Controller
@RequestMapping("/sys/cences/cencesdownloadmember")
public class CencesDownloadMemberController {
    @Autowired
    private CencesDownloadMemberService baseService;

    @Login
    @GetMapping("/list")
    public String list() {
        return "/cencesdownloadmember/list";
    }

    /**
     * 分页显示
     *
     * @param page 页数
     * @param limit 每页条数
     * @param star 开始日期
     * @param end 结束日期
     * @param appStore 渠道
     * @param category 1新用户0老用户2全部
     * @return 返回数据
     */
    @PostMapping("/page")
    @ResponseBody
    @Login
    @DataSource("commonDB")
    public HashMap newlist(String likeBrand,Long page, Integer limit, Integer star, Integer end, String appStore, int category) {
        HashMap map = new HashMap(1);
        CencesDownloadMemberExample example = new CencesDownloadMemberExample();
        CencesDownloadMemberExample.Criteria c = example.createCriteria();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("time desc,id desc");
        if (star != null && end != null) {
            c.andTimeBetween(star, end);
        }
        if(!TextUtil.isEmpty(likeBrand)){
            c.andBrandLike(likeBrand+"%");
            appStore=null;
        }else {
            likeBrand=null;
            if (!TextUtil.isEmpty(appStore)) {
                c.andBrandEqualTo(appStore);
            }else {
                appStore=null;
            }
        }
        c.andCategoryEqualTo(category);
        c.andAppIdEqualTo(MyConfig.getEnv());
        map.put("count", baseService.countByExample(example));
        map.put("code", 0);
        List<CencesDownloadMember> masters = baseService.selectByExample(example);
        map.put("data", masters);
        return map;
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    @DataSource("commonDB")
    public JsonData delete(Integer id) {
        baseService.deleteByTime(baseService.selectById(id).getTime());
        return Callback.success("success", "删除成功");
    }

    @Login
    @PostMapping("/lastCheck")
    @ResponseBody
    public String cen(String date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end = LocalDate.parse(date, fmt);
        baseService.starDataInit(LocalDateTime.of(end.plusDays(1), LocalTime.MIN));
        return "success";
    }


}
